package com.echoesnet.echoesshortvideo.Activities;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.echoesnet.echoesshortvideo.AppController;
import com.echoesnet.echoesshortvideo.Constant.CommonConstant;
import com.echoesnet.echoesshortvideo.Fragments.UserInfoFragment;
import com.echoesnet.echoesshortvideo.Fragments.VerticalVideoFragment;
import com.echoesnet.echoesshortvideo.R;
import com.echoesnet.echoesshortvideo.ViewModels.VideoDetailViewModel;
import com.echoesnet.echoesshortvideo.views.HorizontalViewPager;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.model.VideoOptionModel;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;


public class PlayVideoActivity extends BaseActivity
{
    private static final String TAG = PlayVideoActivity.class.getSimpleName();

    @BindView(R.id.horizontal_view_pager)
    HorizontalViewPager horizontalViewPager;

    private Activity mActivity;

    private VerticalVideoFragment verticalVideoFragment;
    private UserInfoFragment userInfoFragment;

    private VideoDetailViewModel uViewModel;
    private String uid = "";
    private String openSource;
    private String videoId;
    private List<Fragment> fragments = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_video);
        ButterKnife.bind(this);
        mActivity = this;
        openSource = getIntent().getStringExtra("openSource");
        videoId = getIntent().getStringExtra("videoId");

        registerBroReceiver();

        verticalVideoFragment = new VerticalVideoFragment();

        Bundle bundle = new Bundle();
        bundle.putString("openSource", openSource);
        bundle.putString("videoId", videoId);
        if ("1".equals(openSource))
            bundle.putString("toUserId", getIntent().getStringExtra("toUserId"));
        verticalVideoFragment.setArguments(bundle);

        verticalVideoFragment.setOnVerticalPagerActionListener(new VerticalVideoFragment.IOnVerticalPagerActionListener()
        {
            @Override
            public void onPageChange(int position)
            {
                if (horizontalViewPager != null)
                    horizontalViewPager.setCurrentItem(position);
            }

            @Override
            public void onFocus()
            {
                if (userInfoFragment != null)
                {
                    userInfoFragment.refreshFocusState();
                }
            }

            @Override
            public void loadUserInfo(String otherUid)
            {
                if (userInfoFragment != null)
                {
                    userInfoFragment.loadUserInfo(otherUid);
                }
            }
        });

        userInfoFragment = new UserInfoFragment();
        userInfoFragment.setClickListener((state, position) -> {
            if (horizontalViewPager != null)
                horizontalViewPager.setCurrentItem(0);
        });
        userInfoFragment.setOnUserInfoActionListener(new UserInfoFragment.IOnUserInfoActionListener()
        {
            @Override
            public void onFocus()
            {
                if (verticalVideoFragment != null)
                {
                    verticalVideoFragment.refreshFocusState();
                }
            }
        });
        fragments.add(verticalVideoFragment);
        fragments.add(userInfoFragment);

        horizontalViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager())
        {
            @Override
            public Fragment getItem(int position)
            {
                return fragments.get(position);
            }

            @Override
            public int getCount()
            {
                return fragments.size();
            }

//            @Override
//            public boolean isViewFromObject(View view, Object object)
//            {
//                return view == object;
//            }
//
//            @Override
//            public void destroyItem(ViewGroup container, int position, Object object)
//            {
//                super.destroyItem(container, position, object);
//            }
        });

        horizontalViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
        {
            @Override
            public void onPageSelected(int position)
            {
                super.onPageSelected(position);
                AppController.getInstance().videoPlayPagePosition = position;
                Intent intent;
                switch (position)
                {
                    case 0:
//                        intent = new Intent(CommonConstant.EAM_BRD_ACTION_PLAY_VIDEO);
//                        sendBroadcast(intent);
                        if (verticalVideoFragment != null)
                            verticalVideoFragment.onVideoResume();
                        break;
                    case 1:
//                        intent = new Intent(CommonConstant.EAM_BRD_ACTION_PAUSE_VIDEO);
//                        sendBroadcast(intent);
                        if (verticalVideoFragment != null)
                            verticalVideoFragment.onVideoPause();
                        break;
                }
            }
        });
        VideoOptionModel videoOptionModel = new VideoOptionModel(tv.danmaku.ijk.media.player.IjkMediaPlayer.OPT_CATEGORY_PLAYER, "enable-accurate-seek", 1);
        VideoOptionModel videoOptionModel1 = new VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "framedrop", 5);
        List<VideoOptionModel> list = new ArrayList<>();
        list.add(videoOptionModel);
        list.add(videoOptionModel1);
        GSYVideoManager.instance().setOptionModelList(list);

        getWindow().setBackgroundDrawableResource(R.drawable.transparent);
    }


    @Override
    protected void onResume()
    {
        super.onResume();
        if (verticalVideoFragment != null)
        {
            if (horizontalViewPager.getCurrentItem() == 0)
                verticalVideoFragment.onVideoResume();
        }

    }

    @Override
    protected void onPause()
    {
        super.onPause();
        if (verticalVideoFragment != null)
        {
            if (horizontalViewPager.getCurrentItem() == 0)
                verticalVideoFragment.onVideoPause();
        }
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        unRegisterBroReceiver();
    }

    @Override
    public void onBackPressed()
    {
        if( userInfoFragment.backListener())
            return;


        if (horizontalViewPager.getCurrentItem() == 1)
            horizontalViewPager.setCurrentItem(0);
        else
            super.onBackPressed();

    }
    public void setHorizontalViewPagerScroll(boolean noScroll)
    {
        if(this.horizontalViewPager!=null)
            horizontalViewPager.setNoScroll(noScroll);

    }

    private void registerBroReceiver()
    {
        IntentFilter filter = new IntentFilter();
        filter.addAction(CommonConstant.EAM_BRD_ACTION_VIDEO_CHANGE_PAGE);
        mActivity.registerReceiver(receiver, filter);
    }

    private void unRegisterBroReceiver()
    {
        if (receiver != null)
            mActivity.unregisterReceiver(receiver);
    }

    BroadcastReceiver receiver = new BroadcastReceiver()
    {
        @Override
        public void onReceive(Context context, Intent intent)
        {
            switch (intent.getAction())
            {
                case CommonConstant.EAM_BRD_ACTION_VIDEO_CHANGE_PAGE:
                    if (horizontalViewPager != null)
                        horizontalViewPager.setCurrentItem(1);
//                    String userId = intent.getStringExtra("userId");
//                    Intent intent1 = new Intent(CommonConstant.EAM_BRD_ACTION_REQUEST_USERINFO);
//                    intent.putExtra("otherUserId", userId);
//                    mActivity.sendBroadcast(intent1);
                    break;
            }
        }
    };

}
